import edu.princeton.cs.algs4.*;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class Bag<Item> implements Iterable<Item>
{
    private Node<Item> first;
    private int n;

    private static class Node<Item>
    {
        private Item item;
        private Node<Item> next;
    }

    public Bag()
    {
        first = null;
        n = 0;
    }

    public boolean isEmpty()
    {
        return first == null;
    }

    public int size()
    {
        return n;
    }

    public void add(Item item)
    {
        Node<Item> oldfirst = first;
        first = new Node<Item>();
        first.item = item;
        first.next = oldfirst;
        n++;
    }

    public Iterator<Item> iterator()
    {
        return new LinkedIterator(first);
    }

    private class LinkedIterator implements Iterator<Item>
    {
        private Node<Item> current;

        public LinkedIterator(Node<Item> first)
        {
            current = first;
        }
        
        public boolean hasNext()
        {
            return current != null;
        }

        public Item next()
        {
            if(!hasNext())
                throw new NoSuchElementException();
            Item item = current.item;
            current = current.next;
            return item;
        }
    }

    public static void main(String[] args)
    {
        Bag<String> bag = new Bag<String>();
        int n = 15;
        //while(!StdIn.isEmpty())
        for(int i = 0; true; i++)
        {
            String item = StdIn.readString();
            bag.add(item);
            if(i == n)
                break;
        }

        StdOut.println("size of bag = " + bag.size());
        for(String s : bag)
        {
            StdOut.println(s);
        }
    }
}

